package com.koo.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.common.vo.AuditParams;
import com.koo.modules.sys.dao.SysAuditDao;
import com.koo.modules.sys.entity.SysAuditEntity;
import com.koo.modules.sys.service.SysAuditService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("sysAuditService")
public class SysAuditServiceImpl extends ServiceImpl<SysAuditDao, SysAuditEntity> implements SysAuditService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Object key = params.get("key");
        LambdaQueryWrapper<SysAuditEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MyStrUtil.isNotEmpty(key), SysAuditEntity::getTitle, key)
                .or().eq(MyStrUtil.isNotEmpty(key), SysAuditEntity::getOrderNo, key);

        IPage<SysAuditEntity> page = this.page(
                new Query<SysAuditEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Integer updateAudit(AuditParams auditParams) {
        Integer res = 0;
        if(auditParams != null && MyStrUtil.isNotEmpty(auditParams.getIds())){
            String[] idArr = auditParams.getIds().split(",");
            for (String id : idArr){
                LambdaQueryWrapper<SysAuditEntity> qw = new LambdaQueryWrapper<>();
                qw.eq(MyStrUtil.isNotEmpty(id), SysAuditEntity::getOrderId, id);
                SysAuditEntity sysAuditEntity = baseMapper.selectOne(qw);
                sysAuditEntity.setAuditStatus(auditParams.getAuditStatus());

                sysAuditEntity.setTitle(auditParams.getTitle() +"【" + sysAuditEntity.getOrderNo() +"】");
                res += baseMapper.updateById(sysAuditEntity);
            }
        }
        return res;
    }

}